package engine.query;

import storage.record.Schema;


public class ProductPlan implements Plan {
	private Plan p1, p2;
	private Schema schema = new Schema();
	

	public ProductPlan(Plan p1, Plan p2) {
		this.p1 = p1;
		this.p2 = p2;
		schema.addAll(p1.schema());
		schema.addAll(p2.schema());
	}


	public Scan open() {
		Scan s1 = p1.open();
		Scan s2 = p2.open();
		return new ProductScan(s1, s2);
	}

	
	public int blocksAccessed() {
		return p1.blocksAccessed() + (p1.recordsOutput() * p2.blocksAccessed());
	}

	
	public int recordsOutput() {
		return p1.recordsOutput() * p2.recordsOutput();
	}

	
	public int distinctValues(String fldname) {
		if (p1.schema().hasField(fldname))
			return p1.distinctValues(fldname);
		else
			return p2.distinctValues(fldname);
	}

	public Schema schema() {
		return schema;
	}
}
